#! /bin/bash

INTEL_SCRIPT=set_irq_affinity.intel.sh
MELLANOX_SCRIPT=set_irq_affinity.mellanox.sh
INTEL_SCRIPT_ARGS="-x local"
MELLANOX_SCRIPT_ARGS=""

which ip > /dev/null || apt-get install iproute2 -y

interface=$(ip route get 10.10.10.10 | grep -Eo "dev [^\s]+" | head -1 | awk {'print $2'})
[ -z "$interface" ] && echo "Failed to get internal interfaces!" && exit 1

# if using bond
if [ -f /proc/net/bonding/$interface ]; then
	nics=$(cat /sys/class/net/$interface/bonding/slaves)
else
	nics=$interface
fi

# determine interface driver type
for nic in $nics; do
	if ethtool -i $nic | grep ^driver | grep -q mlx5; then
		irq_script=$MELLANOX_SCRIPT
		irq_script_args=$MELLANOX_SCRIPT_ARGS
	elif ethtool -i $nic | grep ^driver | grep -q ice; then
		irq_script=$INTEL_SCRIPT
		irq_script_args=$INTEL_SCRIPT_ARGS
	else
		driver=$(ethtool -i $nic | grep ^driver | awk {'print $2'})
		echo "Unknown ethernet nic driver $driver"
	fi
done


which $irq_script || { echo "No $irq_script script found!" && exit 1; }

$irq_script $irq_script_args $nics